package com.zzyl.nursing.service.impl;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 报警数据Service业务层处理
 *
 * @author syl
 * @date 2025-06-05
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService {
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     *
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById ( Long id ) {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     *
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList ( AlertData alertData ) {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData ( AlertData alertData ) {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     *
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData ( AlertData alertData ) {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     *
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds ( Long[] ids ) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     *
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById ( Long id ) {
        return removeById(id) ? 1 : 0;
    }

    @Override
    public int handleAlertData ( AlertData handleInfo ) {

        AlertData originalAlert = alertDataMapper.selectAlertDataById(handleInfo.getId());
        if (originalAlert == null) {
            return 0;
        }

        Date createTime = originalAlert.getCreateTime();
        String iotId = originalAlert.getIotId();
        Long alertRuleId = originalAlert.getAlertRuleId();

        QueryWrapper<AlertData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("create_time", createTime)
                .eq("iot_id", iotId)
                .eq("alert_rule_id", alertRuleId)
                .eq("status", 0);

        AlertData updateData = new AlertData();
        updateData.setStatus(1);
        updateData.setProcessingResult(handleInfo.getProcessingResult());
        updateData.setProcessingTime(handleInfo.getProcessingTime());
        updateData.setProcessorId(handleInfo.getProcessorId());
        updateData.setProcessorName(handleInfo.getProcessorName());

        return alertDataMapper.update(updateData, queryWrapper);

    }
}